//==============================================================================
//
// Title:       BIST_CheckFlashTest.c
// Purpose:     A short description of the implementation.
//
// Created on:  2013-3-1 at 15:00:35 by Vinson.Zhang
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================
#include "RFBB.h"         
#include <ansi_c.h>

_FRMON_TEST_STRUCT_CMD g_cFRMONFlashTestCmd[6] = {{"FRMon,action,load,agent,FlashAccess,Generic,agentInstance","done"},
                                                        {"FRMon/Generic/agentInstance,action,init,FlashAccess,1","done"},
                                                        {"FRMon/Generic/agentInstance,action,init,FlashAccess,2","done"},
                                                        {"FRMon/Generic/agentInstance,action,init,FlashAccess,3","done"},
                                                        {"FRMon/Generic/agentInstance,action,flashTest","done,0x00"},
                                                        {"FRMon/Generic/agentInstance,action,terminate,FlashAccess","done"}};



/**************************************************************************************************
  Function:         BIST_CheckFlashTest
  Description:      This test aims to check that Flash IC are undamaged.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     FRHD_FlashTest
  Used Command:     g_cFRMONFlashTestCmd
**************************************************************************************************/
void __decldllexp BIST_CheckFlashTest(tTestData *testData, tTestError *testError)
{
    
    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;   
    char* pszStepName = NULL;    
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char cChannel[BUFFER_SIZE_256] ={'\0'};
    int iPipeNum = 0;
    int iTempType = 0;
    int iPassFail = 1;
	int iReturn = 0;
	int error = 0;
    int iArryNum = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
   
    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );
   
    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
    
        //InetTelnetOpen(FRCommand)
    g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);     

    for(iArryNum = 0;iArryNum < 5;iArryNum++)
    	{
        sprintf(kszShmonAgent,g_cFRMONFlashTestCmd[iArryNum].cWriteFrmonCmd); 
        FRmonAgent_String(kszShmonAgent, -1,g_cFRMONFlashTestCmd[iArryNum].cReturnValue, &iPassFail);
        if (iPassFail!=1) 
        	{
            goto Error;
        	}
		Delay(5);
    	}    
    
Error: 

#if(ON_OFF != SIMU_VERS)
    //action terminate
    sprintf(kszShmonAgent,g_cFRMONFlashTestCmd[5].cWriteFrmonCmd);
    Frmon_RunCmdNoAck(kszShmonAgent, 1);

    if(iPassFail == PASS) 
    {
        testData->result = PASS;      
    }
    else 
    {
        testData->result = FAIL;
    }   

	//InetTelnetClose(FRCommand) 
    InetTelnetClose(g_hFrmonAgent);
    
	//FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if (error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }

#else

#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

    return;    
}

